﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="positions.aspx.cs" Inherits="SampleSite.admin.positions" %>

<div>
    <table id="positions" style="height: auto">
    </table>
</div>

<%-- 选择部门 --%>
<div id="dlgOrg" class="easyui-dialog" style="width: 420px; height: 520px; padding: 10px 20px"
    closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <table id="orgGrid" style="width: 370px" title="组织架构" iconcls="icon-edit">
    </table>
    <table style="width: 370px">
        <tr>
            <td style="text-align: right;">
                <a href="javascript:void(0)"
                    class="easyui-linkbutton"
                    id="btnOrg"
                    iconcls="icon-ok">确定</a>
                <a href="javascript:void(0)"
                    class="easyui-linkbutton close_dlg"
                    dlg="dlgOrg"
                    iconcls="icon-cancel">关闭</a>
            </td>
        </tr>
    </table>
</div>

<%-- 添加职位 --%>
<div id="dlgAddPosition" class="easyui-dialog" style="width: 330px; height: 180px; padding: 10px 20px"
    closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="AddPosition" method="post" novalidate="novalidate">
        <table>
            <tr>
                <th colspan="2">添加职位</th>
            </tr>
            <tr>
                <td>
                    <label for="AddP_CName">职位中文名：</label></td>
                <td>
                    <input class="easyui-validatebox" type="text" id="AddP_CName" name="P_CName"
                        style="width: 175px;"
                        data-options="required:true,validType:['chinese']" />
                    <input type="hidden" id="AddP_Org" name="P_Org" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="AddP_EName">职位英文名：</label></td>
                <td>
                    <input class="easyui-validatebox" type="text" id="AddP_EName" name="P_EName"
                        style="width: 175px;"
                        data-options="validType:'english'" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center; padding-top: 10px">
                    <a href="javascript:void(0)"
                        class="easyui-linkbutton"
                        id="btnAddPosition"
                        iconcls="icon-ok">确定</a>
                    <a href="javascript:void(0)"
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgAddPosition"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<%-- 修改职位 --%>
<div id="dlgUpPosition" class="easyui-dialog" style="width: 330px; height: 180px; padding: 10px 20px"
    closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="UpPosition" method="post" novalidate="novalidate">
        <table>
            <tr>
                <th colspan="2">修改职位</th>
            </tr>
            <tr>
                <td>
                    <label for="UpP_CName">职位中文名：</label></td>
                <td>
                    <input class="easyui-validatebox" type="text" id="UpP_CName" name="P_CName"
                        style="width: 175px;"
                        data-options="required:true,validType:['chinese']" />
                    <input type="hidden" id="UpP_Org" name="P_Org" />
                    <input type="hidden" id="UpP_ID" name="P_ID" />
                    <input type="hidden" id="UpP_Level" name="P_Level" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="UpP_EName">职位英文名：</label></td>
                <td>
                    <input class="easyui-validatebox" type="text" id="UpP_EName" name="P_EName"
                        style="width: 175px;"
                        data-options="validType:'english'" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center; padding-top: 10px">
                    <a href="javascript:void(0)"
                        class="easyui-linkbutton"
                        id="btnUpPosition"
                        iconcls="icon-ok">确定</a>
                    <a href="javascript:void(0)"
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgUpPosition"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<%-- 修改职位角色 --%>
<div id="dlgUpPosiRole" class="easyui-dialog" style="width: 450px; height: 450px; padding: 10px 20px"
    closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="UpPosiRole" method="post" novalidate="novalidate">
        <div style="float: left; margin-left: 10px;">
            <table id="Roles" class="easyui-datagrid" style="width: 380px; height: 335px;">
            </table>
        </div>
        <div style="float: left; margin-left: 120px; margin-top: 10px;">
            <a href="javascript:void(0)"
                class="easyui-linkbutton"
                id="btnUpPosiRole"
                iconcls="icon-add">确定</a>
            <a href="javascript:void(0)"
                class="easyui-linkbutton close_dlg"
                dlg="dlgUpPosiRole"
                iconcls="icon-remove">关闭</a>
        </div>
    </form>
</div>

<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件 
        initpositions();

    });

    var dlgOrg = {
        clearOrgDialog: function () {
            initOrgGrid();
        },

        showOrgDialog: function () {
            dlgOrg.clearOrgDialog();
            $('#dlgOrg').dialog('open');
        },

        SelectOrg: function () {
            var rows = $('#orgGrid').treegrid('getSelections');
            if (rows.length < 1) {
                $.messager.alert('提示', '请选择部门！');
            }
            else {
                dlgAddPosition.showAddPositionDialog();
                $('#dlgOrg').dialog('close');
            }
        },

        init: function () {
            $('#btnOrg').click(dlgOrg.SelectOrg);
            $('#dlgOrg').dialog({ title: '选择部门' });
        }
    };



    var dlgAddPosition = {
        clearAddPositionDialog: function () {
            $('#AddP_CName').val('');
            $('#AddP_EName').val('');
            $('#AddP_Org').val('');
        },

        AddPositionInit: function () {
            var rows = $('#orgGrid').treegrid('getSelections');
            $('#AddP_Org').val(rows[0].id);
        },

        showAddPositionDialog: function () {
            dlgAddPosition.clearAddPositionDialog();
            dlgAddPosition.AddPositionInit();
            $('#dlgAddPosition').dialog('open');
        },

        addPosition: function () {
            var validate = $('#AddPosition').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/addPosition.ashx', $('#AddPosition').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '添加成功');

                    $('#dlgAddPosition').dialog('close');
                    $('#positions').datagrid('reload');
                }
                else {
                    $.messager.alert('提示', '添加失败:' + data.msg);
                }
            }, 'json');
        },

        init: function () {
            $('#btnAddPosition').click(dlgAddPosition.addPosition);
            $('#dlgAddPosition').dialog({ title: '添加职位' });
        }
    };

    var dlgUpPosition = {
        clearUpPositionDialog: function () {
            $('#UpP_CName').val('');
            $('#UpP_EName').val('');
            $('#UpP_Org').val('');
            $('#UpP_Level').val('');
            $('#UpP_ID').val('');
        },

        UpPositionInit: function () {
            var rows = $('#positions').datagrid('getSelections');
            $('#UpP_ID').val(rows[0].P_ID);
            $('#UpP_CName').val(rows[0].P_CName);
            $('#UpP_EName').val(rows[0].P_EName);
            $('#UpP_Org').val(rows[0].P_Org);
            $('#UpP_Level').val(rows[0].P_Level);
        },

        showUpPositionDialog: function () {
            dlgUpPosition.clearUpPositionDialog();
            dlgUpPosition.UpPositionInit();
            $('#dlgUpPosition').dialog('open');
        },

        updatePosition: function () {
            var validate = $('#UpPosition').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/updatePosition.ashx', $('#UpPosition').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '添加成功');

                    $('#dlgUpPosition').dialog('close');
                    $('#positions').datagrid('reload');
                }
                else {
                    $.messager.alert('提示', '添加失败:' + data.msg);
                }
            }, 'json');
        },

        init: function () {
            $('#btnUpPosition').click(dlgUpPosition.updatePosition);
            $('#dlgUpPosition').dialog({ title: '修改职位' });
        }
    };


    var dlgUpPosiRole = {
        clearPosiRoleDialog: function () {
            
        },

        showPosiRoleDialog: function () {
            dlgUpPosiRole.clearPosiRoleDialog();
            $('#dlgUpPosiRole').dialog('open');
        },

        updatePosiRole: function () {
            var validate = $('#UpPosiRole').form('validate');
            if (validate == false) {
                return false;
            }

            var row = $('#positions').datagrid('getSelected');
            var row1 = $('#Roles').datagrid('getSelections');
            var myjson = null;
            if (row1.length > 0) {
                var mselect = { rows: [{ P_ID: row.P_ID, R_ID: row1[0].R_ID }] };
                for (var i = 1; i < row1.length; i++) {
                    mselect.rows.push({ P_ID: row.P_ID, R_ID: row1[i].R_ID });
                }
            }
            else {
                var mselect = { rows: [{ P_ID: row.P_ID, R_ID: 0 }] };
            }
            
            myjson = JSON.stringify(mselect.rows);

            $.post('/admin/ajax/updatePosiRole.ashx', { data: myjson }, function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '修改成功');

                        $('#dlgUpPosiRole').dialog('close');
                        $('#PositionRole_'+ row.P_ID).datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '修改失败:' + data.msg);
                    }
                }, 'json');
            
        },

        init: function () {
            $('#btnUpPosiRole').click(dlgUpPosiRole.updatePosiRole);
            $('#dlgUpPosiRole').dialog({ title: '选择角色' });
        }
    };

    function onDelete() {
        var row = $('#positions').datagrid('getSelected');
        if (row) {
            $.messager.confirm(
                "提示", "是否删除当前选择项?",
                function (r) {
                    if (r == true) {
                        $.post(
                            '/admin/ajax/deletePosition.ashx',
                            {
                                Id: row.P_ID
                            },
                            function (data) {
                                if (data.state == 'ok') {
                                    $.messager.alert('提示', '删除成功');
                                    $('#positions').datagrid('reload');
                                }
                                else
                                    $.messager.alert('提示', '删除失败');
                            }, 'json');
                    }
                });
        }
        else {
            $.messager.alert('提示', '请先选择需要删除的职位');
        }
    }

    function initpositions() {
        var lastExpandRow = -1;

        $('#positions').datagrid(
            {
                title: '职位管理',
                rownumbers: true,
                pagination: true,
                idField: 'P_ID',
                singleSelect: true,
                view: detailview,
                url: '/admin/ajax/getPositions.ashx',

                columns: [[
                    { field: 'P_Org', hidden: true },
                    { field: 'P_Org', hidden: true },
                    { field: 'P_Level', hidden: true },
                    { field: 'state', hidden: true },
                    { field: 'P_State', width: 100, title: '状态', formatter: function (v, r, i) { if (v == 1) return '启用'; else return '禁用'; } },
                    { field: 'P_CName', width: 100, title: '职位中文名' },
                    { field: 'P_EName', width: 100, title: '职位英文名' },
                    { field: 'O_CName', width: 100, title: '所属部门' }
                ]],

                onClickRow: function (i, r) {
                    $('#positions').datagrid('expandRow', i);
                    $('#positions').datagrid('fixDetailRowHeight', i);
                },

                detailFormatter: function (i, r) {
                    return '<div style="height:180px;padding:10px;"><table id="PositionRole_' + r.P_ID + '" style="height:180px;"></table><div>';
                },

                onExpandRow: function (i, r) {
                    if (lastExpandRow != -1 && lastExpandRow != i) {
                        $('#positions').datagrid('collapseRow', lastExpandRow);
                    }

                    $('#PositionRole_' + r.P_ID).datagrid({
                        title: '职位角色',
                        rownumbers: true,
                        singleSelect: true,
                        idField: 'P_ID',
                        remoteSort: false,
                        url: '/admin/ajax/getPositionRole.ashx?P_ID=' + r.P_ID,

                        columns: [[
                            { field: 'P_ID', hidden: true },
                            { field: 'R_ID', hidden: true },
                            { field: 'R_State', width: 50, title: "状态" },
                            { field: 'R_CName', width: 100, title: '角色中文名' },
                            { field: 'R_EName', width: 100, align: 'right', title: '角色英文名' },
                            { field: 'R_Description', width: 100, title: '角色说明' }
                        ]]
                    });

                    lastExpandRow = i;
                },

                toolbar: [{
                    id: '_btnAddPosition',
                    text: '添加职位',
                    iconCls: 'icon-add',
                    handler: function () {
                        dlgOrg.showOrgDialog();
                    }
                }, '-', {
                    id: 'btncut',
                    text: '修改',
                    iconCls: 'icon-cut',
                    handler: function () {
                        var rows = $('#positions').datagrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的职位！');
                        }
                        else {
                            dlgUpPosition.showUpPositionDialog();
                        }
                    }
                }, '-', {
                    id: 'btnsave',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        onDelete();
                    }
                }, '-', {
                    id: '_btnEnablePosi',
                    text: '启用/禁用',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#positions').datagrid('getSelections');
                        if (row) {
                            var msg = row[0].P_State == 0 ?
                                '是否启用当前选择职位?' :
                                '是否禁用当前选择职位?';
                            $.messager.confirm(
                                '提示',
                                msg,
                                function (r) {
                                    if (r == false) {
                                        return;
                                    }
                                    $.post('/admin/ajax/enablePosition.ashx',
                                            { P_ID: row[0].P_ID, P_State: row[0].state },
                                            function (data) {
                                                if (data.state == 'ok') {
                                                    $.messager.alert('提示', '操作成功');
                                                }
                                                else {
                                                    $.messager.alert('提示', '操作失败');
                                                }

                                                $('#positions').datagrid('reload');
                                            }, 'json');
                                });
                        }
                        else {
                            $.messager.alert('提示', '请选择职位');
                        }
                    }
                }, '-', {
                    id: 'btnUpPosiRole',
                    text: '修改职位角色',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var rows = $('#positions').datagrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的职位！');
                        }
                        else {
                            $('#Roles').datagrid('options').url = '/admin/ajax/getRoleByPosition.ashx';
                            $('#Roles').datagrid('reload', {
                                P_ID: rows[0].P_ID
                            });
                            dlgUpPosiRole.showPosiRoleDialog();
                        }
                    }
                }]
            });
    }

    function initOrgGrid() {
        $('#orgGrid').treegrid(
            {
                url: '/admin/ajax/getOrg.ashx',
                title: '组织架构',
                rownumbers: true,
                idField: 'id',
                treeField: 'name',
                columns: [[
                    { title: '中文名称', field: 'name', width: 240, sortable: false },
                    {
                        title: '英文名称',
                        field: 'ename',
                        width: 100,
                        sortable: false
                    }
                ]]
            });
    }

    function init_Roles() {
        $('#Roles').datagrid(
            {
                title: '选择角色',
                rownumbers: true,
                idField: 'R_ID',
                columns: [[
                    { field: 'ck', checkbox: true },
                    { field: 'IsHave', hidden: true },
                    { title: '角色中文名', field: 'R_CName', width: 100, sortable: false },
                    { title: '角色英文名', field: 'R_EName', width: 100, sortable: false },
                    { title: '角色说明', field: 'R_Description', width: 100, sortable: false }
                ]],

                onLoadSuccess: function (data) {
                    $('#Roles').datagrid('uncheckAll');
                    for (var i = 0; i < data.rows.length; i++) {
                        if (data.rows[i].IsHave==1)
                            $('#Roles').datagrid('selectRecord', data.rows[i].R_ID);
                    }
                },
            });
    }

    // 绑定各个dialog的关闭事件
    $('.close_dlg').each(function (i, e) {
        var dlg = $(e).attr('dlg');
        $(e).click(function () {
            $('#' + dlg).dialog('close');
        });
    });

    dlgOrg.init();
    dlgAddPosition.init();
    dlgUpPosition.init();
    dlgUpPosiRole.init();
    init_Roles();
</script>
